package com.hongxun.modules.csg.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hongxun.common.utils.*;
import com.hongxun.common.validator.ValidatorUtils;
import com.hongxun.common.validator.group.AddGroup;
import com.hongxun.common.validator.group.UpdateGroup;
import com.hongxun.modules.bss.entity.DPictureEntity;
import com.hongxun.modules.bss.service.DPictureService;
import com.hongxun.modules.csg.entity.*;
import com.hongxun.modules.csg.service.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 隐蔽工程采集记录
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-08-30 10:08:43
 */
@RestController
@RequestMapping("/csg/cmiddlecheck")
public class CMiddleCheckController {
	@Value("${com.hongxun.IMAGEROTT}")
	private String ROOT;
	@Autowired
	private CMiddleCheckService cMiddleCheckService;
	@Autowired
	private DPictureService dPictureService;
	@Autowired
	private CPBaseService cPBaseService;
	@Autowired
	private CBasicsWorkService cBasicsWorkService;
	@Autowired
	private CGroundService cGroundService;
	@Autowired
	private CLinePitService cLinePitService;
	@Autowired
	private CPolePitService cPolePitService;



	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("csg:cmiddlecheck:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        String wheres =" 1=1 ";
		if(query.get("pGuid")!=null&&!query.get("pGuid").toString().equals(""))
		{
			wheres+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
		}else{
			wheres += " and 1=2 ";
		}
		//项目名称
//		if(query.get("pBName")!=null&& !query.get("pBName").toString().equals("")) {
//			wheres += " and  m_c_type != '"+query.get("pBName").toString()+"' ";
//		}


		//记录类别
		if(query.get("mCType")!=null&& !query.get("mCType").toString().equals("")) {

			if(query.get("mCType").toString().equals("全部")){

			}else
				wheres += " and  m_c_type = '"+query.get("mCType").toString()+"' ";
		}
		//记录方式
		if(query.get("mCMode")!=null&& !query.get("mCMode").toString().equals("")) {
			if(query.get("mCMode").toString().equals("全部")){

			}else
				wheres += " and  m_c_mode = '"+query.get("mCMode").toString()+"' ";
		}
		//编号
		if(query.get("mCname")!=null&& !query.get("mCname").toString().equals("")) {
			wheres += " and  m_c_name like'%"+query.get("mCname").toString()+"%' ";
		}
//		if(query.get("pBName")!=null&& !query.get("pBName").toString().equals("")) {
//			wheres += " and  p_name like'%"+query.get("pBName").toString()+"%' ";
//		}
		if(query.get("state")!=null&&query.get("state").toString().equals("isdelno")) {
			wheres += " and  l_p_datastate != '删除' ";
		}


        query.put("filterSql",wheres+" ");

		List<CMiddleCheckEntity> cMiddleCheckList = cMiddleCheckService.queryList(query);

		int total = cMiddleCheckService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(cMiddleCheckList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}


	/**
	 *
	 * @param params
	 * @return
	 */
	@RequestMapping("/select")
	public R select(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		String wheres =" 1=1 ";
		if(query.get("pGuid")!=null&&!query.get("pGuid").toString().equals(""))
		{
			wheres+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
		}

		wheres+=" and  m_c_datastate != '删除' ";
		query.put("filterSql",wheres+" ");
		List<CMiddleCheckEntity> cMiddleCheckEntityList = cMiddleCheckService.queryList(query);
		return R.ok().put("returnlist", cMiddleCheckEntityList);
	}





	/**
	 * 信息
	 */
	@RequestMapping("/info/{guid}")
	@RequiresPermissions("csg:cmiddlecheck:info")
	public R info(@PathVariable("guid") String guid){
		CMiddleCheckEntity cMiddleCheck = cMiddleCheckService.queryObject(guid);
		
		return R.ok().put("cMiddleCheck", cMiddleCheck);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("csg:cmiddlecheck:save")
	public R save(@RequestBody CMiddleCheckEntity cMiddleCheck){
//		ValidatorUtils.validateEntity(cMiddleCheck, AddGroup.class);
		cMiddleCheck.setMCDatastate("新增");
		cMiddleCheckService.save(cMiddleCheck);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("csg:cmiddlecheck:update")
	public R update(@RequestBody CMiddleCheckEntity cMiddleCheck){
		ValidatorUtils.validateEntity(cMiddleCheck, UpdateGroup.class);
		cMiddleCheck.setMCDatastate("修改");
		cMiddleCheckService.update(cMiddleCheck);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("csg:cmiddlecheck:delete")
	public R delete(@RequestBody String[] guids){
		cMiddleCheckService.deleteBatch(guids);
		
		return R.ok();
	}


	/**
	 * 导出中间检查
	 *
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws DocumentException
	 */
	@RequestMapping("/exports")
	public void exports(HttpServletRequest request , HttpServletResponse response) throws IOException, DocumentException {

		// TODO: 2018/9/25  四个子项：拉线坑、接地体、杆塔坑、设备基础

		Map<String , Object> cmiddleMap = new HashMap<>();

		//项目ID
		String pGuid = request.getParameter("pGuid");

		String cmiddleWhere = " 1=1 and p_guid='"+pGuid+"'";
		cmiddleMap.put("filterSql" , cmiddleWhere);
		List<CMiddleCheckEntity> cMiddleCheckEntityList = cMiddleCheckService.queryList(cmiddleMap);

		CPBaseEntity cpBaseEntity = cPBaseService.queryObject(pGuid);

//		CMiddleCheckEntity cMiddleCheckEntity = cMiddleCheckService.queryObject(guid);
		//设置下载文件的格式(表格)msword
//		response.setContentType("application/vnd.ms-excel;charset=utf-8");//表格
		response.setContentType("application/msword;charset=utf-8");//文档
		try {
//			response.setHeader("Content-Disposition", "attachment;filename=" + new String(((cStackPoint.getSPName()==null?"堆放点材料":cStackPoint.getSPName()) + ".xlsx").getBytes(), "iso-8859-1"));
			response.setHeader("Content-Disposition", "attachment;filename=" + new String(((cpBaseEntity.getPBName()==null?"中间检查":cpBaseEntity.getPBName()) + "隐蔽工程记录.doc").getBytes(), "iso-8859-1"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		//A4纸横向打印
		Document document = new Document(PageSize.A4.rotate());
		//字体设置
		BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

		RtfWriter2.getInstance(document, response.getOutputStream());
		Font titleFontye = new Font(bfChinese,8,Font.BOLD);
		HeaderFooter footer = new HeaderFooter(new Phrase("页码：",titleFontye), true);
		footer.setAlignment(1);
		footer.setBorder(Rectangle.NO_BORDER);
		document.setFooter(footer);
		document.open();
		//文件内容及格式
		setexports(document,bfChinese,cMiddleCheckEntityList , pGuid);

		document.close();

	}


	/**
	 *
	 *
	 * @param document
	 * @param bfChinese
	 * @param
	 * @throws DocumentException
	 */
	public void setexports(Document document  ,BaseFont bfChinese ,	List<CMiddleCheckEntity> cMiddleCheckEntityList ,String pGuid ) throws DocumentException {

		Map<String, Object> cMiddleCheckMap = new HashMap<>();

		//1.拉线坑
		String cLineWhere = " 1=1 and p_guid='" + pGuid + "' and l_p_datastate !='删除' ";
		cMiddleCheckMap.put("filterSql", cLineWhere);
		List<CLinePitEntity> cLinePitEntityList = cLinePitService.queryList(cMiddleCheckMap);


		//3.杆塔坑
		cMiddleCheckMap.clear();
		String cPolePitWhere = " 1=1 and p_guid='" + pGuid + "' and p_p_datastate !='删除' ";
		cMiddleCheckMap.put("filterSql", cPolePitWhere);
		List<CPolePitEntity> cPolePitEntityList = cPolePitService.queryList(cMiddleCheckMap);

		//4.设备基础
		cMiddleCheckMap.clear();
		String cBasicsWorkWhere = " 1=1 and p_guid='" + pGuid + "' and b_w_datastate !='删除' ";
		cMiddleCheckMap.put("filterSql", cBasicsWorkWhere);
		List<CBasicsWorkEntity> cBasicsWorkEntityList = cBasicsWorkService.queryList(cMiddleCheckMap);

		CPBaseEntity cpBaseEntity = cPBaseService.queryObject(pGuid);


		//表题字体
		Font titleFont = new Font(bfChinese, 11, Font.BOLD);
		//表格字体
		Font contextFont = new Font(bfChinese, 8, Font.NORMAL);
		//签字字体
		Font qinFont = new Font(bfChinese, 10, Font.NORMAL);
//		Paragraph title = new Paragraph("配网技改/修理项目申请书（适用于总投资100万元以下的配网技改/修理项目）");
//		title.setAlignment(Element.ALIGN_CENTER);
//		title.setFont(titleFont);
//		document.add(title);

//		Table table = new Table(13);
//		int[] withs = {1, 3, 3, 2, 2, 3,3, 2,2, 2, 2, 2,4};
		//  7,11,7,8
		Table table = new Table(10);
		int[] withs = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
		table.setWidths(withs);
		/** 表格所占页面宽度 author:yyli Sep 15, 2010 */
		table.setWidth(90);
		/** 居中显示 author:yyli Sep 15, 2010*/
		table.setAlignment(Element.ALIGN_CENTER);
		/** 自动填满 author:yyli Sep 15, 2010 */
		table.setAutoFillEmptyCells(true);

		OutPutUtils.addcell(table, cpBaseEntity.getPBName() + "项目-隐蔽工程", 0, 11, true, contextFont);




		if (cLinePitEntityList.size() != 0) {
			//1.拉线坑 8
			OutPutUtils.addcell(table, "拉线坑检查项：", 0, 10, false, contextFont);
//			OutPutUtils.addcell(table, "隐蔽工程名称", 0, 2, true, contextFont);
			OutPutUtils.addcell(table, "所属编号/名称", 0, 0, true, contextFont);
			OutPutUtils.addcell(table, "设计坑深", 0, 2, true, contextFont);
			OutPutUtils.addcell(table, "实际坑深", 0, 0, true, contextFont);
			OutPutUtils.addcell(table, "土层深度", 0, 0, true, contextFont);
			OutPutUtils.addcell(table, "沙石深度", 0, 0, true, contextFont);
			OutPutUtils.addcell(table, "检查结果", 0, 2, true, contextFont);
			OutPutUtils.addcell(table, "添加时间", 0, 2, true, contextFont);


		for (CMiddleCheckEntity cMiddleCheckEntity : cMiddleCheckEntityList) {
			for (CLinePitEntity cLinePitEntity : cLinePitEntityList) {
				if (cLinePitEntity.getMCGuid().equals(cMiddleCheckEntity.getGuid())) {
//					OutPutUtils.addcell(table, cLinePitEntity.getMCName(), 0, 2, true, contextFont);
					OutPutUtils.addcell(table, cMiddleCheckEntity.getMCName(), 0, 0, true, contextFont);
					OutPutUtils.addcell(table, cLinePitEntity.getLPDesigndepth(), 0, 2, true, contextFont);
					OutPutUtils.addcell(table, cLinePitEntity.getLPActualdepth(), 0, 0, true, contextFont);
					OutPutUtils.addcell(table, cLinePitEntity.getLPSoil(), 0, 0, true, contextFont);
					OutPutUtils.addcell(table, cLinePitEntity.getLPStone(), 0, 0, true, contextFont);
					OutPutUtils.addcell(table, cLinePitEntity.getLPResult(), 0, 2, true, contextFont);
					OutPutUtils.addcell(table, cLinePitEntity.getLPAddtime() == null ? "" : DateUtils.format(cLinePitEntity.getLPAddtime(), "yyyy-MM-dd"), 0, 2, true, contextFont);
				}
			}
		}
		}

		if (cPolePitEntityList.size() != 0) {
			//3.杆塔坑 8
			OutPutUtils.addcell(table, "杆塔坑检查项：", 0, 10, false, contextFont);
//			OutPutUtils.addcell(table, "隐蔽工程名称", 0, 2, true, contextFont);
			OutPutUtils.addcell(table, "所属编号/名称", 0, 0, true, contextFont);
			OutPutUtils.addcell(table, "设计坑深", 0, 2, true, contextFont);
			OutPutUtils.addcell(table, "实际坑深", 0, 0, true, contextFont);
			OutPutUtils.addcell(table, "土层深度", 0, 0, true, contextFont);
			OutPutUtils.addcell(table, "沙石深度", 0, 0, true, contextFont);
			OutPutUtils.addcell(table, "检查结果", 0, 2, true, contextFont);
			OutPutUtils.addcell(table, "添加时间", 0, 2, true, contextFont);

			for (CMiddleCheckEntity cMiddleCheckEntity : cMiddleCheckEntityList) {
			for (CPolePitEntity cPolePitEntity : cPolePitEntityList) {
				if (cPolePitEntity.getMCGuid().equals(cMiddleCheckEntity.getGuid())) {
//				OutPutUtils.addcell(table, cPolePitEntity.getMCName(), 0, 2, true, contextFont);
					OutPutUtils.addcell(table, cMiddleCheckEntity.getMCName(), 0, 0, true, contextFont);
				OutPutUtils.addcell(table, cPolePitEntity.getPPDesigndepth(), 0, 2, true, contextFont);
				OutPutUtils.addcell(table, cPolePitEntity.getPPActualdepth(), 0, 0, true, contextFont);
				OutPutUtils.addcell(table, cPolePitEntity.getPPSoil(), 0, 0, true, contextFont);
				OutPutUtils.addcell(table, cPolePitEntity.getPPStone(), 0, 0, true, contextFont);
				OutPutUtils.addcell(table, cPolePitEntity.getPPResult(), 0, 2, true, contextFont);
				OutPutUtils.addcell(table, cPolePitEntity.getPPAddtime() == null ? "" : DateUtils.format(cPolePitEntity.getPPAddtime(), "yyyy-MM-dd"), 0, 2, true, contextFont);
				}
			}
		}}

		if (cBasicsWorkEntityList.size() != 0) {
			//4.设备基础 8
			OutPutUtils.addcell(table, "设备基础检查项：", 0, 10, false, contextFont);
//			OutPutUtils.addcell(table, "隐蔽工程名称", 0, 2, true, contextFont);
			OutPutUtils.addcell(table, "所属编号/名称", 0, 0, true, contextFont);
			OutPutUtils.addcell(table, "外形尺寸", 0, 0, true, contextFont);
			OutPutUtils.addcell(table, "基坑实有标高", 0, 0, true, contextFont);
			OutPutUtils.addcell(table, "基坑开挖尺寸", 0, 0, true, contextFont);
			OutPutUtils.addcell(table, "基坑埋设深度", 0, 0, true, contextFont);
			OutPutUtils.addcell(table, "水泥强度等级", 0, 0, true, contextFont);
			OutPutUtils.addcell(table, "检查结果", 0, 2, true, contextFont);
			OutPutUtils.addcell(table, "添加时间", 0, 2, true, contextFont);

			for (CMiddleCheckEntity cMiddleCheckEntity : cMiddleCheckEntityList) {
			for (CBasicsWorkEntity cBasicsWorkEntity : cBasicsWorkEntityList) {
				if (cBasicsWorkEntity.getMCGuid().equals(cMiddleCheckEntity.getGuid())) {
//				OutPutUtils.addcell(table, cBasicsWorkEntity.getMCName(), 0, 2, true, contextFont);
					OutPutUtils.addcell(table, cMiddleCheckEntity.getMCName(), 0, 0, true, contextFont);
				OutPutUtils.addcell(table, cBasicsWorkEntity.getBWContour(), 0, 0, true, contextFont);
				OutPutUtils.addcell(table, cBasicsWorkEntity.getBWHigh(), 0, 0, true, contextFont);
				OutPutUtils.addcell(table, cBasicsWorkEntity.getBWDig(), 0, 0, true, contextFont);
				OutPutUtils.addcell(table, cBasicsWorkEntity.getBWDepth(), 0, 0, true, contextFont);
				OutPutUtils.addcell(table, cBasicsWorkEntity.getBWGrade(), 0, 0, true, contextFont);
				OutPutUtils.addcell(table, cBasicsWorkEntity.getBWResult(), 0, 2, true, contextFont);
				OutPutUtils.addcell(table, cBasicsWorkEntity.getBWAddtime() == null ? "" : DateUtils.format(cBasicsWorkEntity.getBWAddtime(), "yyyy-MM-dd"), 0, 2, true, contextFont);
				}
			}
		}}


		document.add(table);
	}







	/**
	 * 导出中间检查照片
	 *
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws DocumentException
	 */
	@RequestMapping("/exportsimg")
	public void exportsimg(HttpServletRequest request, HttpServletResponse response) throws IOException, DocumentException {

		Map<String , Object> cmiddleMap = new HashMap<>();

		//中间检查ID
		String pGuid = request.getParameter("pGuid");
//		CMiddleCheckEntity cMiddleCheckEntity = cMiddleCheckService.queryObject(guid);
		String cmiddleWhere = " 1=1 and p_guid='"+pGuid+"'";
		cmiddleMap.put("filterSql" , cmiddleWhere);
		List<CMiddleCheckEntity> cMiddleCheckEntityList = cMiddleCheckService.queryList(cmiddleMap);

//		String pGuid = cMiddleCheckEntity.getPGuid();
		CPBaseEntity cpBaseEntity = cPBaseService.queryObject(pGuid);

		response.setContentType("application/msword;charset=utf-8");
		try {
			response.setHeader("Content-Disposition", "attachment;filename=" + new String(((cpBaseEntity.getPBName()==null?"":cpBaseEntity.getPBName()) + "_隐蔽工程照片.doc").getBytes(), "iso-8859-1"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		Document document = new Document();
		BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

		RtfWriter2.getInstance(document, response.getOutputStream());
		Font titleFontye = new Font(bfChinese,8,Font.BOLD);
		HeaderFooter footer = new HeaderFooter(new Phrase("页码：",titleFontye), true);
		footer.setAlignment(1);
		footer.setBorder(Rectangle.NO_BORDER);
		document.setFooter(footer);
		document.open();
		setexportsimg(document,bfChinese,cMiddleCheckEntityList , cpBaseEntity , pGuid);

		document.close();
	}

	/**
	 * 设置表格格式
	 *
	 * @param document
	 * @param bfChinese
	 * @param
	 * @throws DocumentException
	 * @throws IOException
	 */
	public void setexportsimg(Document document  ,BaseFont bfChinese ,	List<CMiddleCheckEntity> cMiddleCheckEntityList ,CPBaseEntity cpBaseEntity , String pGuid ) throws DocumentException, IOException {


		//表题字体
		Font titleFont = new Font(bfChinese,11,Font.BOLD);
		//表格字体
		Font contextFont = new Font(bfChinese, 8, Font.NORMAL);
		// 签字字体
		Font qinFont = new Font(bfChinese, 10, Font.NORMAL);
		Paragraph title = new Paragraph((cpBaseEntity.getPBName()==null?"":cpBaseEntity.getPBName()) + "-隐蔽工程照片");
		title.setAlignment(Element.ALIGN_CENTER);
		title.setFont(titleFont);
		document.add(title);


		//拉线坑
		Map<String ,Object> cLineMap= new HashMap<>();
		String cLineWheres =" 1=1 and p_guid='"+pGuid+"' and l_p_datastate !='删除' ";
		cLineMap.put("filterSql",cLineWheres+" ");
		List<CLinePitEntity> cLinePitEntityList = cLinePitService.queryList(cLineMap);

		//接地体
		Map<String ,Object> cGroundMap= new HashMap<>();
		String cGroundWheres =" 1=1 and p_guid='"+pGuid+"' and g_datastate !='删除' ";
		cGroundMap.put("filterSql",cGroundWheres+" ");
		List<CGroundEntity> cGroundEntityList = cGroundService.queryList(cGroundMap);

		//杆塔坑
		Map<String ,Object> cPolePitMap= new HashMap<>();
		String cPolePitWheres =" 1=1 and p_guid='"+pGuid+"' and p_p_datastate !='删除' ";
		cPolePitMap.put("filterSql",cPolePitWheres+" ");
		List<CPolePitEntity> cPolePitEntityList = cPolePitService.queryList(cPolePitMap);

		//设备基础
		Map<String ,Object> cBasicsWorkMap= new HashMap<>();
		String cBasicsWorkWheres =" 1=1 and p_guid='"+pGuid+"' and b_w_datastate !='删除' ";
		cBasicsWorkMap.put("filterSql",cBasicsWorkWheres+" ");
		List<CBasicsWorkEntity> cBasicsWorkEntityList = cBasicsWorkService.queryList(cBasicsWorkMap);

		addCLinePitImg(document, contextFont, cLinePitEntityList ,cMiddleCheckEntityList, pGuid);
		addCGroundImg(document,pGuid, contextFont, cGroundEntityList , cMiddleCheckEntityList);
		addCPolePitImg(document,pGuid, contextFont, cPolePitEntityList , cMiddleCheckEntityList);
		addCBasicsWorkImg(document,pGuid, contextFont, cBasicsWorkEntityList , cMiddleCheckEntityList);
	}

	/**
	 *	拉线照片
	 *
	 * @param document
	 * @param
	 * @param contextFont
	 * @param
	 * @throws DocumentException
	 * @throws IOException
	 */
	private void addCLinePitImg(Document document, Font contextFont,
								List<CLinePitEntity> cLinePitEntityList ,
								List<CMiddleCheckEntity> cMiddleCheckEntityList ,
								String pGuid) throws DocumentException, IOException {

		for (CMiddleCheckEntity cMiddleCheckEntity : cMiddleCheckEntityList) {


		for (CLinePitEntity cLinePitEntity : cLinePitEntityList) {
			if (cLinePitEntity.getMCGuid().equals(cMiddleCheckEntity.getGuid())) {
			Map<String, Object> cScheduleMap = new HashMap<>();
			String cScheduleWhere = " 1=1 and p_guid= '" + pGuid +
					"' and p_status = '施工' and p_label like'%检查%'" +
					" and p_p_guid='" + cLinePitEntity.getGuid() + "' and p_datastate !='删除' ";
			cScheduleMap.put("filterSql", cScheduleWhere + " ");
			List<DPictureEntity> dPictureList = dPictureService.queryListForPlabel(cScheduleMap);
			if (dPictureList.size() > 0) {

				Table table = new Table(6);
				int[] withs = {1, 2, 1, 2, 1, 2};
				table.setWidths(withs);
				/** 表格所占页面宽度 author:yyli Sep 15, 2010 */
				table.setWidth(90);
				/** 居中显示 author:yyli Sep 15, 2010 */
				table.setAlignment(Element.ALIGN_CENTER);
				/** 自动填满 author:yyli Sep 15, 2010 */
				table.setAutoFillEmptyCells(true);

				OutPutUtils.addcell(table, cLinePitEntity.getMCName() + "(检查)", 0, 6, true, contextFont);
				int chishu = dPictureList.size() / 3;
				int yushu = dPictureList.size() % 3;

				if (yushu > 0) {
					chishu = chishu + 1;
				}
				//一行显示3张照片
				for (int i = 0; i < chishu; i++) {
					boolean one = false;
					boolean two = false;
					boolean three = false;

					if ((3 * i) < dPictureList.size()) {
						one = true;
					}
					if (((3 * i) + 1) < dPictureList.size()) {
						two = true;
					}
					if (((3 * i) + 2) < dPictureList.size()) {
						three = true;
					}
					if (one) {
						File file = new File(ROOT + dPictureList.get(3 * i).getPServeraddress());
						if (file.exists()) {
							OutPutUtils.addcellimg(table, ROOT + dPictureList.get(3 * i).getPServeraddress(), 8, 2, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);
						}
					} else {
						OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
					}
					if (two) {
						File file = new File(ROOT + dPictureList.get((3 * i) + 1).getPServeraddress());
						if (file.exists()) {
							OutPutUtils.addcellimg(table, ROOT + dPictureList.get((3 * i) + 1).getPServeraddress(), 8, 2, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);
						}
					} else {
						OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
					}
					if (three) {
						File file = new File(ROOT + dPictureList.get((3 * i) + 2).getPServeraddress());
						if (file.exists()) {
							OutPutUtils.addcellimg(table, ROOT + dPictureList.get((3 * i) + 2).getPServeraddress(), 8, 2, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);
						}
					} else {
						OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
					}

					//照片名称
					OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
					if (one) {
						if (dPictureList.get(3 * i).getPLabel().equals("检查,xc")) {
							OutPutUtils.addcell(table, "现场照片", 0, 0, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "图纸照片", 0, 0, true, contextFont);
						}
//						OutPutUtils.addcell(table, dPictureList.get(3 * i).getPLabel(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
					if (two) {
						if (dPictureList.get((3 * i) + 1).getPLabel().equals("检查,xc")) {
							OutPutUtils.addcell(table, "现场照片", 0, 0, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "图纸照片", 0, 0, true, contextFont);
						}
//						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPLabel(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
					if (three) {
						if (dPictureList.get((3 * i) + 2).getPLabel().equals("检查,xc")) {
							OutPutUtils.addcell(table, "现场照片", 0, 0, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "图纸照片", 0, 0, true, contextFont);
						}
//						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPLabel(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}


					//所属编号
					OutPutUtils.addcell(table, "所属编号", 0, 0, true, contextFont);
					if (one) {
						if (dPictureList.get(3 * i).getPPGuid().equals(cLinePitEntity.getGuid())) {
							OutPutUtils.addcell(table, cMiddleCheckEntity.getMCName(), 0, 0, true, contextFont);
						}
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "所属编号", 0, 0, true, contextFont);
					if (two) {
						if (dPictureList.get((3 * i) + 1).getPPGuid().equals(cLinePitEntity.getGuid())) {
							OutPutUtils.addcell(table, cMiddleCheckEntity.getMCName(), 0, 0, true, contextFont);
						}
//						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPLabel(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "所属编号", 0, 0, true, contextFont);
					if (three) {
						if (dPictureList.get((3 * i) + 2).getPPGuid().equals(cLinePitEntity.getGuid())) {
							OutPutUtils.addcell(table, cMiddleCheckEntity.getMCName(), 0, 0, true, contextFont);
						}
//						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPLabel(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}


					//照片来源：相册or相机
					OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
					if (one) {
						OutPutUtils.addcell(table, dPictureList.get(3 * i).getPPResour(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
					if (two) {
						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPPResour(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
					if (three) {
						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPPResour(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}

					//时间
					OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
					if (one) {
						OutPutUtils.addcell(table, dPictureList.get(3 * i).getPTime() == null ? "" : DateUtils.format(dPictureList.get(3 * i).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
					if (two) {
						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPTime() == null ? "" : DateUtils.format(dPictureList.get((3 * i) + 1).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
					if (three) {
						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPTime() == null ? "" : DateUtils.format(dPictureList.get((3 * i) + 2).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
				}
				document.add(table);
			}
		}
		}
	}
	}


	/**
	 *  接地体照片
	 *
	 * @param document
	 * @param pGuid
	 * @param contextFont
	 * @param cGroundEntityList
	 * @throws DocumentException
	 * @throws IOException
	 */
	private void addCGroundImg(Document document, String pGuid, Font contextFont,
							   List<CGroundEntity> cGroundEntityList ,
							   List<CMiddleCheckEntity> cMiddleCheckEntityList) throws DocumentException, IOException {

		for (CMiddleCheckEntity cMiddleCheckEntity : cMiddleCheckEntityList) {


		for (CGroundEntity cGroundEntity : cGroundEntityList) {
			if (cGroundEntity.getMCGuid().equals(cMiddleCheckEntity.getGuid())) {
			Map<String, Object> cScheduleMap = new HashMap<>();
			String cScheduleWhere = " 1=1 and p_guid= '" + pGuid +
					"' and p_status = '施工' and p_label like'%检查%'" +
					" and p_p_guid='" + cGroundEntity.getGuid() + "' and p_datastate !='删除' ";
			cScheduleMap.put("filterSql", cScheduleWhere + " ");
			List<DPictureEntity> dPictureList = dPictureService.queryListForPlabel(cScheduleMap);
			if (dPictureList.size() > 0) {

				Table table = new Table(6);
				int[] withs = {1, 2, 1, 2, 1, 2};
				table.setWidths(withs);
				/** 表格所占页面宽度 author:yyli Sep 15, 2010 */
				table.setWidth(90);
				/** 居中显示 author:yyli Sep 15, 2010 */
				table.setAlignment(Element.ALIGN_CENTER);
				/** 自动填满 author:yyli Sep 15, 2010 */
				table.setAutoFillEmptyCells(true);

				OutPutUtils.addcell(table, cGroundEntity.getMCName() + "(检查)", 0, 6, true, contextFont);
				int chishu = dPictureList.size() / 3;
				int yushu = dPictureList.size() % 3;

				if (yushu > 0) {
					chishu = chishu + 1;
				}
				//一行显示3张照片
				for (int i = 0; i < chishu; i++) {
					boolean one = false;
					boolean two = false;
					boolean three = false;

					if ((3 * i) < dPictureList.size()) {
						one = true;
					}
					if (((3 * i) + 1) < dPictureList.size()) {
						two = true;
					}
					if (((3 * i) + 2) < dPictureList.size()) {
						three = true;
					}
					if (one) {
						File file = new File(ROOT + dPictureList.get(3 * i).getPServeraddress());
						if (file.exists()) {
							OutPutUtils.addcellimg(table, ROOT + dPictureList.get(3 * i).getPServeraddress(), 8, 2, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);
						}
					} else {
						OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
					}
					if (two) {
						File file = new File(ROOT + dPictureList.get((3 * i) + 1).getPServeraddress());
						if (file.exists()) {
							OutPutUtils.addcellimg(table, ROOT + dPictureList.get((3 * i) + 1).getPServeraddress(), 8, 2, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);
						}
					} else {
						OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
					}
					if (three) {
						File file = new File(ROOT + dPictureList.get((3 * i) + 2).getPServeraddress());
						if (file.exists()) {
							OutPutUtils.addcellimg(table, ROOT + dPictureList.get((3 * i) + 2).getPServeraddress(), 8, 2, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);
						}
					} else {
						OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
					}

					//照片名称
					OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
					if (one) {
						if (dPictureList.get(3 * i).getPLabel().equals("检查,xc")) {
							OutPutUtils.addcell(table, "现场照片", 0, 0, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "图纸照片", 0, 0, true, contextFont);
						}
//						OutPutUtils.addcell(table, dPictureList.get(3 * i).getPLabel(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
					if (two) {
						if (dPictureList.get((3 * i) + 1).getPLabel().equals("检查,xc")) {
							OutPutUtils.addcell(table, "现场照片", 0, 0, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "图纸照片", 0, 0, true, contextFont);
						}
//						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPLabel(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
					if (three) {
						if (dPictureList.get((3 * i) + 2).getPLabel().equals("检查,xc")) {
							OutPutUtils.addcell(table, "现场照片", 0, 0, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "图纸照片", 0, 0, true, contextFont);
						}
//						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPLabel(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}


					//所属编号
					OutPutUtils.addcell(table, "所属编号", 0, 0, true, contextFont);
					if (one) {
						if (dPictureList.get(3 * i).getPPGuid().equals(cGroundEntity.getGuid())) {
							OutPutUtils.addcell(table, cMiddleCheckEntity.getMCName(), 0, 0, true, contextFont);
						}
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "所属编号", 0, 0, true, contextFont);
					if (two) {
						if (dPictureList.get((3 * i) + 1).getPPGuid().equals(cGroundEntity.getGuid())) {
							OutPutUtils.addcell(table, cMiddleCheckEntity.getMCName(), 0, 0, true, contextFont);
						}
//						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPLabel(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "所属编号", 0, 0, true, contextFont);
					if (three) {
						if (dPictureList.get((3 * i) + 2).getPPGuid().equals(cGroundEntity.getGuid())) {
							OutPutUtils.addcell(table, cMiddleCheckEntity.getMCName(), 0, 0, true, contextFont);
						}
//						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPLabel(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}


					//照片来源：相册or相机
					OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
					if (one) {
						OutPutUtils.addcell(table, dPictureList.get(3 * i).getPPResour(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
					if (two) {
						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPPResour(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
					if (three) {
						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPPResour(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}

					//时间
					OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
					if (one) {
						OutPutUtils.addcell(table, dPictureList.get(3 * i).getPTime() == null ? "" : DateUtils.format(dPictureList.get(3 * i).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
					if (two) {
						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPTime() == null ? "" : DateUtils.format(dPictureList.get((3 * i) + 1).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
					if (three) {
						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPTime() == null ? "" : DateUtils.format(dPictureList.get((3 * i) + 2).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
				}
				document.add(table);
			}
		}
		}
		}
	}


	/**
	 * 	杆塔坑照片
	 *
	 * @param document
	 * @param
	 * @param contextFont
	 * @param cPolePitEntityList
	 * @throws DocumentException
	 * @throws IOException
	 */
	private void addCPolePitImg(Document document, String pGuid, Font contextFont,
								List<CPolePitEntity> cPolePitEntityList ,
								List<CMiddleCheckEntity> cMiddleCheckEntityList) throws DocumentException, IOException {

		for (CMiddleCheckEntity cMiddleCheckEntity : cMiddleCheckEntityList) {


		for (CPolePitEntity cPolePitEntity : cPolePitEntityList) {
			if (cPolePitEntity.getMCGuid().equals(cMiddleCheckEntity.getGuid())) {
			Map<String, Object> cScheduleMap = new HashMap<>();
			String cScheduleWhere = " 1=1 and p_guid= '" + pGuid +
					"' and p_status = '施工' and p_label like'%检查%'" +
					" and p_p_guid='" + cPolePitEntity.getGuid() + "' and p_datastate !='删除' ";
			cScheduleMap.put("filterSql", cScheduleWhere + " ");
			List<DPictureEntity> dPictureList = dPictureService.queryListForPlabel(cScheduleMap);
			if (dPictureList.size() > 0) {

				Table table = new Table(6);
				int[] withs = {1, 2, 1, 2, 1, 2};
				table.setWidths(withs);
				/** 表格所占页面宽度 author:yyli Sep 15, 2010 */
				table.setWidth(90);
				/** 居中显示 author:yyli Sep 15, 2010 */
				table.setAlignment(Element.ALIGN_CENTER);
				/** 自动填满 author:yyli Sep 15, 2010 */
				table.setAutoFillEmptyCells(true);

				OutPutUtils.addcell(table, cPolePitEntity.getMCName() + "(检查)", 0, 6, true, contextFont);
				int chishu = dPictureList.size() / 3;
				int yushu = dPictureList.size() % 3;

				if (yushu > 0) {
					chishu = chishu + 1;
				}
				//一行显示3张照片
				for (int i = 0; i < chishu; i++) {
					boolean one = false;
					boolean two = false;
					boolean three = false;

					if ((3 * i) < dPictureList.size()) {
						one = true;
					}
					if (((3 * i) + 1) < dPictureList.size()) {
						two = true;
					}
					if (((3 * i) + 2) < dPictureList.size()) {
						three = true;
					}
					if (one) {
						File file = new File(ROOT + dPictureList.get(3 * i).getPServeraddress());
						if (file.exists()) {
							OutPutUtils.addcellimg(table, ROOT + dPictureList.get(3 * i).getPServeraddress(), 8, 2, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);
						}
					} else {
						OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
					}
					if (two) {
						File file = new File(ROOT + dPictureList.get((3 * i) + 1).getPServeraddress());
						if (file.exists()) {
							OutPutUtils.addcellimg(table, ROOT + dPictureList.get((3 * i) + 1).getPServeraddress(), 8, 2, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);
						}
					} else {
						OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
					}
					if (three) {
						File file = new File(ROOT + dPictureList.get((3 * i) + 2).getPServeraddress());
						if (file.exists()) {
							OutPutUtils.addcellimg(table, ROOT + dPictureList.get((3 * i) + 2).getPServeraddress(), 8, 2, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);
						}
					} else {
						OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
					}

					//照片名称
					OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
					if (one) {
						if (dPictureList.get(3 * i).getPLabel().equals("检查,xc")) {
							OutPutUtils.addcell(table, "现场照片", 0, 0, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "图纸照片", 0, 0, true, contextFont);
						}
//						OutPutUtils.addcell(table, dPictureList.get(3 * i).getPLabel(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
					if (two) {
						if (dPictureList.get((3 * i) + 1).getPLabel().equals("检查,xc")) {
							OutPutUtils.addcell(table, "现场照片", 0, 0, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "图纸照片", 0, 0, true, contextFont);
						}
//						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPLabel(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
					if (three) {
						if (dPictureList.get((3 * i) + 2).getPLabel().equals("检查,xc")) {
							OutPutUtils.addcell(table, "现场照片", 0, 0, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "图纸照片", 0, 0, true, contextFont);
						}
//						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPLabel(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}


					//所属编号
					OutPutUtils.addcell(table, "所属编号", 0, 0, true, contextFont);
					if (one) {
						if (dPictureList.get(3 * i).getPPGuid().equals(cPolePitEntity.getGuid())) {
							OutPutUtils.addcell(table, cMiddleCheckEntity.getMCName(), 0, 0, true, contextFont);
						}
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "所属编号", 0, 0, true, contextFont);
					if (two) {
						if (dPictureList.get((3 * i) + 1).getPPGuid().equals(cPolePitEntity.getGuid())) {
							OutPutUtils.addcell(table, cMiddleCheckEntity.getMCName(), 0, 0, true, contextFont);
						}
//						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPLabel(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "所属编号", 0, 0, true, contextFont);
					if (three) {
						if (dPictureList.get((3 * i) + 2).getPPGuid().equals(cPolePitEntity.getGuid())) {
							OutPutUtils.addcell(table, cMiddleCheckEntity.getMCName(), 0, 0, true, contextFont);
						}
//						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPLabel(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}


					//照片来源：相册or相机
					OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
					if (one) {
						OutPutUtils.addcell(table, dPictureList.get(3 * i).getPPResour(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
					if (two) {
						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPPResour(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
					if (three) {
						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPPResour(), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}

					//时间
					OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
					if (one) {
						OutPutUtils.addcell(table, dPictureList.get(3 * i).getPTime() == null ? "" : DateUtils.format(dPictureList.get(3 * i).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
					if (two) {
						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPTime() == null ? "" : DateUtils.format(dPictureList.get((3 * i) + 1).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
					OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
					if (three) {
						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPTime() == null ? "" : DateUtils.format(dPictureList.get((3 * i) + 2).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
					} else {
						OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
					}
				}
				document.add(table);
			}
		}
		}
		}
	}


	/**
	 * 设备基础照片
	 *
	 * @param document
	 * @param
	 * @param contextFont
	 * @param cBasicsWorkEntityList
	 * @throws DocumentException
	 * @throws IOException
	 */
	private void addCBasicsWorkImg(Document document, String pGuid, Font contextFont,
								   List<CBasicsWorkEntity> cBasicsWorkEntityList ,
								   List<CMiddleCheckEntity> cMiddleCheckEntityList) throws DocumentException, IOException {

		for (CMiddleCheckEntity cMiddleCheckEntity : cMiddleCheckEntityList) {


		for (CBasicsWorkEntity cBasicsWorkEntity : cBasicsWorkEntityList) {
			if (cBasicsWorkEntity.getMCGuid().equals(cMiddleCheckEntity.getGuid())) {


				Map<String, Object> cScheduleMap = new HashMap<>();
				String cScheduleWhere = " 1=1 and p_guid= '" + pGuid +
						"' and p_status = '施工' and p_label like'%检查%'" +
						" and p_p_guid='" + cBasicsWorkEntity.getGuid() + "' and p_datastate !='删除' ";
				cScheduleMap.put("filterSql", cScheduleWhere + " ");
				List<DPictureEntity> dPictureList = dPictureService.queryListForPlabel(cScheduleMap);
				if (dPictureList.size() > 0) {

					Table table = new Table(6);
					int[] withs = {1, 2, 1, 2, 1, 2};
					table.setWidths(withs);
					/** 表格所占页面宽度 author:yyli Sep 15, 2010 */
					table.setWidth(90);
					/** 居中显示 author:yyli Sep 15, 2010 */
					table.setAlignment(Element.ALIGN_CENTER);
					/** 自动填满 author:yyli Sep 15, 2010 */
					table.setAutoFillEmptyCells(true);

					OutPutUtils.addcell(table, cBasicsWorkEntity.getMCName() + "(检查)", 0, 6, true, contextFont);
					int chishu = dPictureList.size() / 3;
					int yushu = dPictureList.size() % 3;

					if (yushu > 0) {
						chishu = chishu + 1;
					}
					//一行显示3张照片
					for (int i = 0; i < chishu; i++) {
						boolean one = false;
						boolean two = false;
						boolean three = false;

						if ((3 * i) < dPictureList.size()) {
							one = true;
						}
						if (((3 * i) + 1) < dPictureList.size()) {
							two = true;
						}
						if (((3 * i) + 2) < dPictureList.size()) {
							three = true;
						}
						if (one) {
							File file = new File(ROOT + dPictureList.get(3 * i).getPServeraddress());
							if (file.exists()) {
								OutPutUtils.addcellimg(table, ROOT + dPictureList.get(3 * i).getPServeraddress(), 8, 2, true, contextFont);
							} else {
								OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);
							}
						} else {
							OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
						}
						if (two) {
							File file = new File(ROOT + dPictureList.get((3 * i) + 1).getPServeraddress());
							if (file.exists()) {
								OutPutUtils.addcellimg(table, ROOT + dPictureList.get((3 * i) + 1).getPServeraddress(), 8, 2, true, contextFont);
							} else {
								OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);
							}
						} else {
							OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
						}
						if (three) {
							File file = new File(ROOT + dPictureList.get((3 * i) + 2).getPServeraddress());
							if (file.exists()) {
								OutPutUtils.addcellimg(table, ROOT + dPictureList.get((3 * i) + 2).getPServeraddress(), 8, 2, true, contextFont);
							} else {
								OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);
							}
						} else {
							OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
						}

						//照片名称
						OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
						if (one) {
							if (dPictureList.get(3 * i).getPLabel().equals("检查,xc")) {
								OutPutUtils.addcell(table, "现场照片", 0, 0, true, contextFont);
							} else {
								OutPutUtils.addcell(table, "图纸照片", 0, 0, true, contextFont);
							}
						} else {
							OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
						}
						OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
						if (two) {
							if (dPictureList.get((3 * i) + 1).getPLabel().equals("检查,xc")) {
								OutPutUtils.addcell(table, "现场照片", 0, 0, true, contextFont);
							} else {
								OutPutUtils.addcell(table, "图纸照片", 0, 0, true, contextFont);
							}
//						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPLabel(), 0, 0, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
						}
						OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
						if (three) {
							if (dPictureList.get((3 * i) + 2).getPLabel().equals("检查,xc")) {
								OutPutUtils.addcell(table, "现场照片", 0, 0, true, contextFont);
							} else {
								OutPutUtils.addcell(table, "图纸照片", 0, 0, true, contextFont);
							}
//						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPLabel(), 0, 0, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
						}


						//所属编号
						OutPutUtils.addcell(table, "所属编号", 0, 0, true, contextFont);
						if (one) {
							if (dPictureList.get(3 * i).getPPGuid().equals(cBasicsWorkEntity.getGuid())) {
								OutPutUtils.addcell(table, cMiddleCheckEntity.getMCName(), 0, 0, true, contextFont);
							}
						} else {
							OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
						}
						OutPutUtils.addcell(table, "所属编号", 0, 0, true, contextFont);
						if (two) {
							if (dPictureList.get((3 * i) + 1).getPPGuid().equals(cBasicsWorkEntity.getGuid())) {
								OutPutUtils.addcell(table, cMiddleCheckEntity.getMCName(), 0, 0, true, contextFont);
							}
//						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPLabel(), 0, 0, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
						}
						OutPutUtils.addcell(table, "所属编号", 0, 0, true, contextFont);
						if (three) {
							if (dPictureList.get((3 * i) + 2).getPPGuid().equals(cBasicsWorkEntity.getGuid())) {
								OutPutUtils.addcell(table, cMiddleCheckEntity.getMCName(), 0, 0, true, contextFont);
							}
//						OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPLabel(), 0, 0, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
						}


						//照片来源：相册or相机
						OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
						if (one) {
							OutPutUtils.addcell(table, dPictureList.get(3 * i).getPPResour(), 0, 0, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
						}
						OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
						if (two) {
							OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPPResour(), 0, 0, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
						}
						OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
						if (three) {
							OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPPResour(), 0, 0, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
						}

						//时间
						OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
						if (one) {
							OutPutUtils.addcell(table, dPictureList.get(3 * i).getPTime() == null ? "" : DateUtils.format(dPictureList.get(3 * i).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
						}
						OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
						if (two) {
							OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPTime() == null ? "" : DateUtils.format(dPictureList.get((3 * i) + 1).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
						}
						OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
						if (three) {
							OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPTime() == null ? "" : DateUtils.format(dPictureList.get((3 * i) + 2).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
						} else {
							OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
						}
					}
					document.add(table);
				}
			}
		}
		}
	}







	/**
	 * 导出接地体
	 *
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws DocumentException
	 */
	@RequestMapping("/exportsCground")
	public void exportsCground(HttpServletRequest request , HttpServletResponse response) throws IOException, DocumentException {

		// TODO: 2018/9/25  四个子项：拉线坑、接地体、杆塔坑、设备基础


		//堆放点ID
		String guid = request.getParameter("guid");
		CMiddleCheckEntity cMiddleCheckEntity = cMiddleCheckService.queryObject(guid);

		String pGuid = cMiddleCheckEntity.getPGuid();
		CPBaseEntity cpBaseEntity = cPBaseService.queryObject(pGuid);

		//设置下载文件的格式(表格)msword
//		response.setContentType("application/vnd.ms-excel;charset=utf-8");//表格
		response.setContentType("application/msword;charset=utf-8");//文档
		try {
//			response.setHeader("Content-Disposition", "attachment;filename=" + new String(((cStackPoint.getSPName()==null?"堆放点材料":cStackPoint.getSPName()) + ".xlsx").getBytes(), "iso-8859-1"));
			response.setHeader("Content-Disposition", "attachment;filename=" + new String(((cMiddleCheckEntity.getMCName()==null?"中间检查":cpBaseEntity.getPBName()+cMiddleCheckEntity.getMCName()) + "-接地体.doc").getBytes(), "iso-8859-1"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		//A4纸横向打印
		Document document = new Document(PageSize.A4.rotate());
		//字体设置
		BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

		RtfWriter2.getInstance(document, response.getOutputStream());
		Font titleFontye = new Font(bfChinese,8,Font.BOLD);
		HeaderFooter footer = new HeaderFooter(new Phrase("页码：",titleFontye), true);
		footer.setAlignment(1);
		footer.setBorder(Rectangle.NO_BORDER);
		document.setFooter(footer);
		document.open();
		//文件内容及格式
		setexportsCground(document,bfChinese,cMiddleCheckEntity);

		document.close();

	}


	/**
	 * 接地体设置
	 *
	 * @param document
	 * @param bfChinese
	 * @param
	 * @throws DocumentException
	 */
	public void setexportsCground(Document document  ,BaseFont bfChinese ,	CMiddleCheckEntity cMiddleCheckEntity  ) throws DocumentException {

		Map<String ,Object> cMiddleCheckMap = new HashMap<>();


		//2.接地体
		cMiddleCheckMap.clear();
		String cGroundWhere = " 1=1 and m_c_guid='"+cMiddleCheckEntity.getGuid()+"' and g_datastate !='删除' ";
		cMiddleCheckMap.put("filterSql",cGroundWhere);
		List<CGroundEntity> cGroundEntityList = cGroundService.queryList(cMiddleCheckMap);


		CPBaseEntity cpBaseEntity = cPBaseService.queryObject(cMiddleCheckEntity.getPGuid());


		//表题字体
		Font titleFont = new Font(bfChinese,11,Font.BOLD);
		//表格字体
		Font contextFont = new Font(bfChinese, 8, Font.NORMAL);
		//签字字体
		Font qinFont = new Font(bfChinese, 10, Font.NORMAL);
//		Paragraph title = new Paragraph("配网技改/修理项目申请书（适用于总投资100万元以下的配网技改/修理项目）");
//		title.setAlignment(Element.ALIGN_CENTER);
//		title.setFont(titleFont);
//		document.add(title);

//		Table table = new Table(13);
//		int[] withs = {1, 3, 3, 2, 2, 3,3, 2,2, 2, 2, 2,4};
		//  7,11,7,8
		Table table = new Table(11);
		int[] withs = {1,1,1,1,1,1,1,1,1,1,1};
		table.setWidths(withs);
		/** 表格所占页面宽度 author:yyli Sep 15, 2010 */
		table.setWidth(90);
		/** 居中显示 author:yyli Sep 15, 2010*/
		table.setAlignment(Element.ALIGN_CENTER);
		/** 自动填满 author:yyli Sep 15, 2010 */
		table.setAutoFillEmptyCells(true);

		OutPutUtils.addcell(table, cpBaseEntity.getPBName()+"项目-"+cMiddleCheckEntity.getMCName()+"隐蔽工程", 0, 11, true, contextFont);


		//2.接地体 11
//		OutPutUtils.addcell(table, "2.接地体检查项：", 0, 11, false, contextFont);
		OutPutUtils.addcell(table, "隐蔽工程类别", 0, 0, true, contextFont);
		OutPutUtils.addcell(table, "接地体规格  ", 0, 0, true, contextFont);
		OutPutUtils.addcell(table, "土质情况    ", 0, 0, true, contextFont);
		OutPutUtils.addcell(table, "焊接质量    ", 0, 0, true, contextFont);
		OutPutUtils.addcell(table, "防腐处理    ", 0, 0, true, contextFont);
		OutPutUtils.addcell(table, "槽沟长度    ", 0, 0, true, contextFont);
		OutPutUtils.addcell(table, "槽沟宽度    ", 0, 0, true, contextFont);
		OutPutUtils.addcell(table, "接地电阻值  ", 0, 0, true, contextFont);
		OutPutUtils.addcell(table, "槽沟深度    ", 0, 0, true, contextFont);
		OutPutUtils.addcell(table, "检查结果    ", 0, 0, true, contextFont);
		OutPutUtils.addcell(table, "添加时间    ", 0, 0, true, contextFont);

		for(CGroundEntity cGroundEntity :cGroundEntityList)
		{
			OutPutUtils.addcell(table, cGroundEntity.getMCName(), 0, 0, true, contextFont);
			OutPutUtils.addcell(table, cGroundEntity.getGFormat(), 0, 0, true, contextFont);
			OutPutUtils.addcell(table, cGroundEntity.getGSoil(), 0, 0, true, contextFont);
			OutPutUtils.addcell(table, cGroundEntity.getGMass(), 0, 0, true, contextFont);
			OutPutUtils.addcell(table, cGroundEntity.getGDam(), 0, 0, true, contextFont);
			OutPutUtils.addcell(table, cGroundEntity.getGLength(), 0, 0, true, contextFont);
			OutPutUtils.addcell(table, cGroundEntity.getGWidth(), 0, 0, true, contextFont);
			OutPutUtils.addcell(table, cGroundEntity.getGResistance(), 0, 0, true, contextFont);
			OutPutUtils.addcell(table, cGroundEntity.getGDepth(), 0, 0, true, contextFont);
			OutPutUtils.addcell(table, cGroundEntity.getGResult(), 0, 0, true, contextFont);
			OutPutUtils.addcell(table, cGroundEntity.getGAddtime()==null?"": DateUtils.format(cGroundEntity.getGAddtime(),"yyyy-MM-dd"), 0, 0, true, contextFont);
		}

		document.add(table);
	}







	
}
